// pages/location/userAdd/userAdd.ts
Page({
    /**
     * 页面的初始数据
     */
    data: {
        linkman: '',
        linemanTip: '',
        sex: '0',
        phone: '',
        phoneTip: '',
        location: '',
        locationTip: '',
        isDefault: false
    },

    add() {
        const { linkman, phone, sex, location, isDefault } = this.data
        console.log(linkman, phone, sex, location, isDefault)
        if (!this.valid()) {
            return
        }
    },

    valid() {
        const { linkman, phone, location } = this.data

        if (linkman === '') {
            this.setData({
                linemanTip: '联系人不能为空'
            })
            return false
        } else {
            this.setData({
                linemanTip: ''
            })
        }
        if (phone === '') {
            this.setData({
                phoneTip: '手机号不能为空'
            })
            return false
        } else if (!this.checkTelephone(phone)) {
            this.setData({
                phoneTip: '请输入正确的手机号'
            })
            return false
        } else {
            this.setData({
                phoneTip: ''
            })
        }

        if (location === '') {
            this.setData({
                locationTip: '详细地址不能为空'
            })
            return false
        } else {
            this.setData({
                locationTip: ''
            })
        }
        return true
    },

    checkTelephone(telephone: string) {
        const reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
        return reg.test(telephone)
    },

    handlerIsDefault(SwitchValue: any) {
        console.log(SwitchValue)
        this.setData({
            isDefault: SwitchValue.detail.value
        })
    },

    chooseLocation() {
        wx.chooseLocation({
            success: res => {
                console.log(res)
                this.setData({
                    location: res.address
                })
            },
            fail: (err: any) => {
                console.log(err)
            }
        })
    },

    handlerSexChange(RadioValue: any) {
        console.log(RadioValue)
        this.setData({
            sex: RadioValue.detail.value
        })
    },

    handlerPhoneInput(InputValue: any) {
        console.log(InputValue)
        this.setData({
            phone: InputValue.detail.value
        })
    },

    handlerLinkManInput(InputValue: any) {
        console.log(InputValue)
        this.setData({
            linkman: InputValue.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option: any) {
        if (option.location) {
            this.setData({
                location: option.location
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
})
